package edu.wit.system.controller;

import edu.wit.common.annotation.Log;
import edu.wit.common.api.domain.SysOperLog;
import edu.wit.common.core.web.BaseController;
import edu.wit.common.core.web.page.TableDataInfo;
import edu.wit.common.enums.BusinessType;
import edu.wit.common.result.R;
import edu.wit.common.utils.poi.ExcelUtil;
import edu.wit.system.service.SysFileService;
import edu.wit.system.service.SysOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/sys/operlog")
@Api(tags = "操作日志")
public class SysOperLogController extends BaseController {
    @Autowired
    private SysOperLogService operLogService;

    @Autowired
    private SysFileService sysFileService;

    @ApiOperation("上传图片到Minio")
    @PostMapping("/uploadImg")
    public R testImg(MultipartFile file) throws Exception{
        return R.ok().data("imgurl",sysFileService.uploadFile(file));
    }

//    @ApiOperation("上传minio图片")
//    @PostMapping("/uploadMinioImg")
//    public R uploadMinioImg(MultipartFile file) throws Exception{
//        return R.ok().data("imgurl",sysFileService.uploadFile(file));
//    }

//    @ApiOperation("获取图片")
//    @PostMapping(value = "getImage")
//    public R ftpFile(@RequestParam("imgUrl") String imgUrl) {
//        String download = "data:image/jpeg;base64,";
//        Map<String, String> stringMap = StringUtils.imgUrlToFtp(imgUrl);
//        download += FtpUtil.download(stringMap.get("addr"),stringMap.get("filePath"),stringMap.get("fileName"));
//        System.out.println("imgUrl=" + imgUrl);
//        return R.ok().data("img",download);
//    }

    @ApiOperation("获取操作日志")
    @GetMapping("/list")
    public TableDataInfo list(SysOperLog operLog) {
        startPage();
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        return getDataTable(list);
    }

    @ApiOperation("导出操作日志")
    @Log(title = "导出操作日志", businessType = BusinessType.EXPORT)
//    @PreAuthorize(hasPermi = "system:operlog:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOperLog operLog) throws IOException {
        List<SysOperLog> list = operLogService.selectOperLogList(operLog);
        ExcelUtil<SysOperLog> util = new ExcelUtil<SysOperLog>(SysOperLog.class);
        util.exportExcel(response, list, "操作日志");
    }

    @ApiOperation("删除操作日志")
    //@Log(title = "删除操作日志", businessType = BusinessType.DELETE)
//    @PreAuthorize(hasPermi = "system:operlog:remove")
    @DeleteMapping("/{operIds}")
    public R remove(@PathVariable Long[] operIds)
    {
        return toAjax(operLogService.deleteOperLogByIds(operIds));
    }

//    @PreAuthorize(hasPermi = "system:operlog:remove")
    @ApiOperation("清理操作日志")
    //@Log(title = "清理操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R clean() {
        operLogService.cleanOperLog();
        return R.ok();
    }

    @ApiOperation("保存操作日志")
    @PostMapping("/save")
    public R saveLog(@RequestBody SysOperLog operLog) {
//        System.out.println("operlog-controller" + operLog);
        return toAjax(operLogService.insertOperlog(operLog));
    }

}
